import store from '@/store'
import { getUserInfo,getPermissions} from "@/utils/auth";

function authPermission(permission) {
  // var p = getPermissions();
  // var u = getUserInfo();  
  console.log(getPermissions(),getUserInfo())
  const permissions = JSON.parse(getPermissions());
  const user = JSON.parse(getUserInfo());
  console.log(JSON.stringify(permissions),permission,permissions.permission !== undefined)
  console.log("user",JSON.stringify(user))
  if(user){
    if(user.isAdmin == 1) return true;
    if(user.isSuper == 1) return true;
  }
  if (permission) {
    return permission in permissions ;//permissions.permission !== undefined
  } else {
    return false
  }
}

function authRole(role) {
  const super_admin = "admin";
  const roles = store.getters && store.getters.roles
  if (role && role.length > 0) {
    return roles.some(v => {
      return super_admin === v || v === role
    })
  } else {
    return false
  }
}

export default {
  // 验证用户是否具备某权限
  hasPermi(permission) {
    return authPermission(permission);
  },
  // 验证用户是否含有指定权限，只需包含其中一个
  hasPermiOr(permissions) {
    return authPermission(permissions)
    // return permissions.some(item => {
      
    // })
  },
  // 验证用户是否含有指定权限，必须全部拥有
  hasPermiAnd(permissions) {
    return permissions.every(item => {
      return authPermission(item)
    })
  },
  // 验证用户是否具备某角色
  hasRole(role) {
    return authRole(role);
  },
  // 验证用户是否含有指定角色，只需包含其中一个
  hasRoleOr(roles) {
    return roles.some(item => {
      return authRole(item)
    })
  },
  // 验证用户是否含有指定角色，必须全部拥有
  hasRoleAnd(roles) {
    return roles.every(item => {
      return authRole(item)
    })
  }
}
